
'-------------------------------------------------------------
'Example Procedures prior to Hands-On 4-1
'-------------------------------------------------------------

Sub AgeCalc ()
	' variable declaration
	Dim FullName As String
	Dim DateOfBirth As Date
	Dim age As Integer

	' assign values to variables
	FullName = "John Smith"
	DateOfBirth = #01/03/1967#

	' calculate age
	age = Year(Now())-Year(DateOfBirth)

	' print results to the Immediate Window
	Debug.Print FullName & " is " & age & " years old."
End Sub


Sub HowManyRows()
'this procedure will produce an error 
'due to the wrong data type (demonstration only)
'see the next procedure on how to correct this problem
    
    Dim NumOfRows As Integer
        
    NumOfRows = Rows.Count
    MsgBox "The worksheet has " & NumOfRows & " rows."
End Sub


Sub HowManyRows2()
    Dim NumOfRows As Long
    NumOfRows = Rows.Count
    MsgBox "The worksheet has " & NumOfRows & " rows."
End Sub


Sub MyNumber()
    Dim myNum As Integer
    myNum = 23.11
    MsgBox myNum
End Sub


Sub AgeCalc2()
    ' variable declaration
    Dim FullName$
    Dim DateOfBirth As Date
    Dim age%

    ' assign values to variables
    FullName$ = "John Smith"
    DateOfBirth = #1/3/1967#

    ' calculate age
    age% = Year(Now()) - Year(DateOfBirth)

    ' print results to the Immediate Window
    Debug.Print FullName$ & " is " & age% & " years old."
End Sub


'---------------------------------------------------
' Hands-On 4-1
'---------------------------------------------------

Sub CalcCost()
    slsPrice = 35
    slsTax = 0.085

    Range("A1").Formula = "The cost of calculator"
    Range("A4").Formula = "Price"
    Range("B4").Formula = slsPrice
    Range("A5").Formula = "Sales Tax"
    Range("A6").Formula = "Cost"
    Range("B5").Formula = slsPrice * slsTax
    cost = slsPrice + (slsPrice * slsTax)

    With Range("B6")
        .Formula = cost
        .NumberFormat = "0.00"
    End With

    strMsg = "The calculator total is $" & cost & "."
    Range("A8").Formula = strMsg
End Sub


Sub CalcCost() ' Revised version (after changes made in steps 9-11)

    slsPrice = 35
    slsTax = 0.085

    Range("A1").Formula = "The cost of calculator"
    Range("A4").Formula = "Price"
    Range("B4").Formula = slsPrice
    Range("A5").Formula = "Sales Tax"
    Range("A6").Formula = "Cost"
    Range("B5").Formula = Format((slsPrice * slsTax), "0.00")
    cost = Format(slsPrice + (slsPrice * slsTax), "0.00")

    Range("B6").Formula = cost
  

    strMsg = "The calculator total is $" & cost & "."
    Range("A8").Formula = strMsg
End Sub


'---------------------------------------------------
' Hands-On 4-2
'---------------------------------------------------

Sub CalcCost() 
    ' declaration of variables
    Dim slsPrice As Currency
    Dim slsTax As Single
    Dim cost As Currency
    Dim strMsg As String

    slsPrice = 35
    slsTax = 0.085
    
    Range("A1").Formula = "The cost of calculator"
    Range("A4").Formula = "Price"
    Range("B4").Formula = slsPrice
    Range("A5").Formula = "Sales Tax"
    Range("A6").Formula = "Cost"
    Range("B5").Formula = Format((slsPrice * slsTax), "0.00")
    cost = Format(slsPrice + (slsPrice * slsTax), "0.00")

    Range("B6").Formula = cost
    strMsg = "The calculator total is $" & cost & "."
    Range("A8").Formula = strMsg
End Sub


'---------------------------------------------------
' Hands-On 4-3
'---------------------------------------------------
' make sure to declare slsTax variable at the top of the Module

Sub ExpenseRep()
    Dim slsPrice As Currency
    Dim cost As Currency

    slsPrice = 55.99

    cost = slsPrice + (slsPrice * slsTax)
    MsgBox slsTax
    MsgBox cost
End Sub


'---------------------------------------------------
' Hands-On 4-4
'---------------------------------------------------

Sub CostOfPurchase()
    ' declare variables
    Static allPurchase
    Dim newPurchase As String
    Dim purchCost As Single

    newPurchase = InputBox("Enter the cost of a purchase:")
    purchCost = CSng(newPurchase)
    allPurchase = allPurchase + purchCost

    ' display results
    MsgBox "The cost of a new purchase is: " & newPurchase
    MsgBox "The running cost is: " & allPurchase
End Sub


'---------------------------------------------------
' Hands-On 4-5
'---------------------------------------------------

Sub UseObjVariable()
    Dim myRange As Object
    Set myRange = Worksheets("Sheet1").Range(Cells(1, 1), Cells(10, 5))
    myRange.BorderAround Weight:=xlMedium

    With myRange.Interior
    .ColorIndex = 6
    .Pattern = xlSolid
    End With

    Set myRange = Worksheets("Sheet1").Range(Cells(12, 5), Cells(12, 10))
    myRange.Value = 54

    Debug.Print IsObject(myRange)
End Sub


'---------------------------------------------------
' Hands-On 4-6
' No code in this Hands-On. 
' Please follow the instructions in the book.
'---------------------------------------------------


'---------------------------------------------------
' Hands-On 4-7
' No code in this Hands-On. 
' Please follow the instructions in the book.
'---------------------------------------------------


'---------------------------------------------------
' Hands-On 4-8
' Code recorded by the Macro Recorder.
' Please follow the instructions in the book.
'---------------------------------------------------


Sub MiniWindow()
'
' MiniWindow Macro
'

'
    ActiveWindow.WindowState = xlMinimized
End Sub


